﻿using HokjesSluiten;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HokjesSluitenBot
{
    class Client
    {
        Random rand;
        Map map;
        int mapWidth = 2;
        int mapHeight = 2;

        bool running = true;

        public Client()
        {
            string[] arguments;
            string[] coordArgs;

            try
            {
                //standaard intializatie
                StreamManager manager = new StreamManager();
                rand = new Random();

                while (running)
                {
                    string command = manager.readStream();
                    if (!string.IsNullOrEmpty(command))
                    {
                        // parse de reactie
                        arguments = command.Split(' ');
                        
                        switch (arguments[0])
                        {
                            case Const.START:
                                coordArgs = arguments[1].Split(';');
                                mapWidth = int.Parse(coordArgs[0]);
                                mapHeight = int.Parse(coordArgs[1]);

                                map = new Map(mapWidth, mapHeight);
                                break;

                            case Const.POINT:
                                coordArgs = arguments[1].Split(';');
                                string[] coordOne = coordArgs[0].Split(',');
                                string[] coordTwo = coordArgs[1].Split(',');
                                Point streep1 = new Point(int.Parse(coordOne[0]), int.Parse(coordOne[1]));
                                Point streep2 = new Point(int.Parse(coordTwo[0]), int.Parse(coordTwo[1]));
                                List<Streep> result = map.addStreep(streep1, streep2);
                                break;

                            case Const.RETRY:
                            case Const.TURN:
                                while (true)
                                {
                                    int xPos = rand.Next(0, mapWidth + 1);
                                    int yPos = rand.Next(0, mapHeight + 1);

                                    streep1 = new Point(xPos, yPos);
                                    streep2 = new Point(-1, -1);

                                    
                                    if (xPos + 1 <= mapWidth && GetRandomBoolean()) streep2 = new Point(xPos + 1, yPos);
                                    if (yPos + 1 <= mapHeight && !GetRandomBoolean()) streep2 = new Point(xPos, yPos + 1);
                                    if (streep2.X == -1 || streep2.Y == -1)
                                    {
                                        if (xPos - 1 >= 0 && GetRandomBoolean()) streep2 = new Point(xPos - 1, yPos);
                                        if (yPos - 1 >= 0 && !GetRandomBoolean()) streep2 = new Point(xPos, yPos - 1);
                                    }

                                    result = map.addStreep(streep1, streep2);
                                    if (result != null)
                                    {
                                        manager.writeStream( Const.POINT + " " + String.Join(";", streep1.X + "," + streep1.Y, streep2.X + "," + streep2.Y));
                                        break;
                                    }
                                }
                                break;
                        }
                    }

                    int hokjes = map.hokjesLeft();
                    int moves = map.movesLeft();
                    if (map.hokjesLeft() == 0 || map.movesLeft() == 0)
                        running = false;
                }
            }
            catch (Exception ex)
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter("test.txt", true);
                file.WriteLine(ex.Message);
                file.WriteLine(ex.InnerException);
                file.WriteLine(ex.StackTrace);
                file.Close();
            }
        }

        public bool GetRandomBoolean()
        {
            return rand.Next(0, 2) == 0;
        }
    }
}
